﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.FactoryInfo;

namespace FwProject.Manager
{
    public class FactoryNameInfoManager: IDomainService
    {
        private readonly IRepository<FactoryNameInfo, Guid> _factoryRepository;
        public FactoryNameInfoManager(IRepository<FactoryNameInfo, Guid> factoryRepository)
        {
            _factoryRepository = factoryRepository;

        }
        /// <summary>
        /// 添加一个工厂
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task<Guid> CreateFactory(FactoryNameInfo factory)
        {

            var model = _factoryRepository.GetAll().Where(o => o.FactoryName == factory.FactoryName).FirstOrDefault();
            if (model != null)
                throw new UserFriendlyException("当前工厂已存在");

            return await _factoryRepository.InsertAndGetIdAsync(factory);
        }
        /// <summary>
        ///修改一个工厂信息
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task UpdateFactory(FactoryNameInfo factory)
        {

            await _factoryRepository.UpdateAsync(factory);
        }
        /// <summary>
        /// 查询一工厂
        /// </summary>
        /// <returns>The by identifier factory name info.</returns>
        /// <param name="id">Identifier.</param>
        public async Task<FactoryNameInfo> GetByIdFactoryNameInfo(Guid id)
        {
            return await _factoryRepository.GetAsync(id);
        }
        /// <summary>
        ///  delete 一个工厂
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task DeleteFactory(FactoryNameInfo factory)
        {

            await _factoryRepository.DeleteAsync(factory);
        }
    }
}
